<?php

namespace app\api\controller;

use app\services\CommentService;
use app\services\LikeService;
use app\services\UserCollectService;
use app\services\VideoService;
use support\Db;
use support\Request;

class VideoController
{
    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $userId = $request->post('user_id', get_user_id());

            $data = VideoService::list($page, $length, $userId);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function recommendList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = VideoService::recommendList($page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function hotList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = VideoService::hotList($page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function raceList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = VideoService::raceList($page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function followList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = VideoService::followList($page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function likeVideoList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $userId = $request->post('user_id', get_user_id());

            $data = LikeService::videoList($userId, $page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function collectVideoList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $userId = $request->post('user_id', get_user_id());

            $data = UserCollectService::videoList($userId, $page, $length);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function detail(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            $data = VideoService::detail($id);

            return ok_rsp('成功', $data);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function add(Request $request)
    {
        try {
            $file = $request->file('file');
            $desc = $request->post('desc');

            $extension = $file->getUploadExtension();

            if (!in_array($extension, ['mp4', 'avi', 'flv', 'mov', 'mkv', 'rmvb'])) {
                return err_rsp('不正确的文件扩展');
            }

            $relativePath = '/userUpload/' . microtime(true) * 1000 . '.' . $extension;
            $file->move(public_path() . $relativePath);

            $id = VideoService::add($relativePath, $desc);

            VideoService::generateThumb($id, public_path() . $relativePath);

            return ok_rsp('成功', ['url' => getenv('APP_URL') . $relativePath, 'id' => $id]);

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function like(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::like($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function cancelLike(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::cancelLike($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function collect(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::collect($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function cancelCollect(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::cancelCollect($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function follow(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::follow($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function cancelFollow(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            VideoService::cancelFollow($id);

            return ok_rsp('成功');

        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addComment(Request $request)
    {
        Db::beginTransaction();
        try {
            $id = $request->post('id');
            $commentId = $request->post('comment_id', 0);
            $content = $request->post('content');
            $callUserIds = $request->post('user_ids', []);

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            if (empty(trim($content))) {
                throw new \Exception('评论内容不能为空');
            }

            $data = CommentService::add($id, $content, $commentId, $callUserIds);

            Db::commit();
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }

    public function commentList(Request $request)
    {
        try {
            $id = $request->post('id');
            $page = $request->post('page', 1);

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            $list = CommentService::list($id, $page, 10);

            return ok_rsp('成功', $list);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function likeComment(Request $request)
    {
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            CommentService::likeComment($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function cancelLikeComment(Request $request)
    {
        Db::beginTransaction();
        try {
            $id = $request->post('id');

            if (empty($id)) {
                throw new \Exception('ID不能为空');
            }

            CommentService::cancelLikeComment($id);

            Db::commit();
            return ok_rsp('成功');
        } catch (\Exception $e) {
            Db::rollBack();
            return err_rsp($e->getMessage());
        }
    }

}